<?php
class Integral_mdl extends CI_Model{
  //操作的表.
  const TAB_NAME='clickrcms_integral';
  const TAB_DESC='clickrcms_integral_history';
  function __construct(){
    parent::__construct();
    log_message('debug','integral_mdl Model class Initialized');
  }

  /****
   *查詢所integral,加分页
   *$data :查询参数.
   */
  public function get_integral($data=array()){
    extract($data);
    isset($field) && isset($sort)?$this->db->order_by($field.' '.$sort):$this->db->order_by('id DESC');
    isset($id_array) && !empty($id_array)?$this->db->where_in("id",$id_array):'';
    isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
    isset($send_date) && !empty($send_date)?$this->db->where('send_date >=',strtotime($send_date.' 00:00:00')):'';
    isset($send_date) && !empty($send_date)?$this->db->where('send_date <=',strtotime($send_date.' 23:59:59')):'';
    isset($admin_name) && !empty($admin_name)?$this->db->like('admin_name',$admin_name):'';
    isset($send_result) && !empty($send_result)?$this->db->like('send_result',$send_result):'';
    isset($phone_area) && !empty($phone_area)?$this->db->where('phone_area',$phone_area):'';
    isset($begin_month) && !empty($begin_month) && isset($end_month) && !empty($end_month)?$this->db->where('send_date BETWEEN "'.$begin_month.'" AND "'.$end_month.'"',NULL,FALSE):'';//allocation,index
    return $this->db->get(self::TAB_NAME);
  }
  /****
   * 統計所有的integral
   */
  public function totle_integral($data=array()){
    if(!empty($data)){
      extract($data);
      isset($id_array) && !empty($id_array)?$this->db->where_in("id",$id_array):'';
      isset($send_date) && !empty($send_date)?$this->db->where('send_date >=',strtotime($send_date.' 00:00:00')):'';
      isset($send_date) && !empty($send_date)?$this->db->where('send_date <=',strtotime($send_date.' 23:59:59')):'';
      isset($admin_name) && !empty($admin_name)?$this->db->like('admin_name',$admin_name):'';
      isset($send_result) && !empty($send_result)?$this->db->like('send_result',$send_result):'';
      isset($phone_area) && !empty($phone_area)?$this->db->where('phone_area',$phone_area):'';
      isset($begin_month) && !empty($begin_month) && isset($end_month) && !empty($end_month)?$this->db->where('send_date BETWEEN "'.$begin_month.'" AND "'.$end_month.'"',NULL,FALSE):'';//allocation,index
      return $this->db->count_all_results(self::TAB_NAME);
    }else{
      return $this->db->count_all(self::TAB_NAME);
    }
  }
  
  /*****
   * 根據ID來刪除
   */
  public function delete_integral($delete_id=''){
    if(!empty($delete_id)){
      $this->db->where("integral_id",$delete_id);
      $this->db->delete(self::TAB_DESC);
      $this->db->where("id",$delete_id);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }

  /*****
   * 根據ID數組來刪除 批量刪除
   */
  public function delete_batch($batch=array()){
    if(!empty($batch)){
      $this->db->where_in("integral_id",$batch);
      $this->db->delete(self::TAB_DESC);
      $this->db->where_in("id",$batch);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }

  /***
   * 修改一個integral
   * @param array $data
   * @param int  $edit_id
   * return :受影响的行数
   */
  public function edit_integral($data=array(),$edit_id=0){
    if(!empty($data) && !empty($edit_id)){
      $this->db->where('id',$edit_id);
      $this->db->update(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }

  /***
   * 新增一個 integral
   */
  public function add_integral($data=array()){
    if(!empty($data)){
      $this->db->insert(self::TAB_NAME,$data);
      return $this->db->insert_id();
    }
  }

  /****
   *根据ID来查询得到一个 integral.
   *$id :要查詢的ID
   */
  public function get_integral_by_id($id){
    $data=array();
    $this->db->where('id',$id);
    $get_result=$this->db->get(self::TAB_NAME);
    if($get_result->num_rows()>0){
      $data=$get_result->row_array();
    }
    $get_result->free_result();
    return $data;
  }

  /****
   *根据$filter_item来查询得到一个信息.
   *$filter_item :篩選條件
   */
  public function get_one_integral($filter_item=array()){
    if(!empty($filter_item) && is_array($filter_item)){
      $item=array();
      $this->db->where($filter_item);
      $query=$this->db->get(self::TAB_NAME);
      if($query->num_rows()>0){
        $item=$query->row_array();
      }
      $query->free_result();
      return $item;
    }
  }

  /**
    * 检查是否存在相同{用户名/昵称/邮箱}
    * 
    * @access public
    * @param int - $key {name,screenName,mail}
    * @param int - $value {用户名/昵称/邮箱}的值
    * @param int - $exclude_uid 需要排除的uid
    * @return boolean - success/failure
    */  
  public function check_exist($key = 'phone',$value = '', $exclude_uid = 0)
  {
    if(!empty($key) && !empty($value))
    {
      //fix issue 2
      $this->db->select('id')->from(self::TAB_NAME)->where($key, $value);
      
      if(!empty($exclude_uid) && is_numeric($exclude_uid))
      {
        $this->db->where('id <>', $exclude_uid);  
      }
      
      $query = $this->db->get();
      $num = $query->num_rows();
      
      $query->free_result();
      
      return ($num > 0) ? TRUE : FALSE;
    }
    
    return FALSE;    
  }

}